<?php


namespace addons\official\controller\web;


use addons\official\model\OfficialListOnlineMessage;
use think\Request;
use think\Validate;

class OfficialOnlineMessage extends OfficialBaseController
{
    protected $model;
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        $this->model = new OfficialListOnlineMessage();
        parent::__construct($request);
    }

    /**
     * 详情
     */
    public function list()
    {
        $id = (int)input('param.sid');
        if (!$id) {
            $this->error('栏目必须存在');
        }
        $this->setBasic();
        //查詢父级
        $res = $this->get_cate_pid_in_sid();
        if ($this->request->isPost()) {
            $token = $this->request->post('__token__');
            //验证Token
            if (!$token || !\think\Validate::is($token, "token", ['__token__' => $token])) {
                return json_encode(['code' => 0, 'msg' => '非法请求,请刷新页面']);
            }

            $param = input('param.');
            $rule = [
                "name"    => 'require',
                "phone"   => 'require',
                "title"   => 'require',
                "content" => 'require',
                "captcha" => 'require|captcha',
            ];
            $msg = [
                "name.require"    => __l('online_message', 'Name') . __l('index', 'No_empty'),
                "phone.require"   => __l('online_message', 'Phone') . __l('index', 'No_empty'),
                "title.require"   => __l('online_message', 'Title') . __l('index', 'No_empty'),
                "content.require" => __l('online_message', 'Content') . __l('index', 'No_empty'),
                "captcha.require" => __l('index', 'Captcha') . __l('index', 'No_empty'),
                "captcha.captcha" => __l('index', 'The verification code is incorrect'),
            ];
            $data = [
                "name"                 => $param['name'],
                "phone"                => $param['phone'],
                "title"                => $param['title'],
                "content"              => $param['content'],
                "captcha"              => $param['captcha'],
                "official_category_id" => $id,
                "weigh"                => time()
            ];
            //验证规则
            $validate = new Validate($rule, $msg);
            $result = $validate->check($data);
            if (!$result) {
                return json_encode(['code' => 0, 'msg' => $validate->getError()]);
            }
            unset($data['captcha']);
            //增加数据
            $official_list_form = $this->model->create($data);
            if ($official_list_form) {
                return json_encode(['code' => 1, 'msg' => __l('index', 'Submit successfully')]);
            }
        }

        $this->view->assign('captchaType', config('fastadmin.user_register_captcha'));
        return view("web/official_online_message/list{$this->Mobile}", compact('res'));
    }


}
